#!/usr/bin/perl -w
use strict;
use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");

my ($cmd);

print STDERR <<"HEADLINE";
888888888888888888888888888888888888888888888888888888888888888888888

    table_longest_transcript

	Description:
		The longest transcript for each gene
	Populates DB tables:
		taxon.longest_transcript

888888888888888888888888888888888888888888888888888888888888888888888

HEADLINE

my ($curr_dir) = $FindBin::Bin;
$curr_dir =~ s/(.*)\//$1/;

#-----------------------------------------------------------------------------------------
#	Get DB parameters
#
use db_parameters;
#
#	Get DB parameters
#-----------------------------------------------------------------------------------------

my $dbh = connect_to_panda();
use get_psql_time;
my $start_time = get_psql_time($dbh);


#
#	Download peptide and cDNA files if necessary
#
$dbh->do('TRUNCATE taxon.gene_longest_transcript;');
{
	$dbh->do(<<"PL/SQLCMD");
	INSERT INTO 
        taxon.gene_longest_transcript(gene_id, prot_id, species)
        SELECT 
            DISTINCT ON (gene_id) 
            gene_id, 
            prot_id,
            species
        FROM 
            ens_id NATURAL JOIN 
            sequences
        ORDER BY 
            gene_id, 
            len DESC;
PL/SQLCMD
}


log_pipeline_stage($dbh, 'NULL', 24, 'table longest_transcript', $start_time);

$dbh->disconnect;
